using System;
using System.Runtime;
using System.Runtime.Serialization;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Xml;

namespace PickGold.Web
{
    public class CNamedString : NameValueCollection
    {
        public CNamedString() : base() { }//1
        public CNamedString(IEqualityComparer equalityComparer) : base(equalityComparer) { }//2
        public CNamedString(int capacity) : base(capacity) { }//3
        public CNamedString(NameValueCollection col) : base(col) { }//4
        //public CNamedString(IHashCodeProvider hashProvider,IComparer comparer) : base(hashProvider, comparer) { }//5
        public CNamedString(int capacity, IEqualityComparer equalityComparer) : base(capacity, equalityComparer) { }//6
        public CNamedString(int capacity, NameValueCollection col) : base(capacity, col) { }//7
        public CNamedString(SerializationInfo info, StreamingContext context) : base(info, context) { }//8
        //public CNamedString(int capacity, IHashCodeProvider hashProvider,IComparer comparer) : base(capacity, hashProvider, comparer) { }//9
        public CNamedString(string queryString) : base(HttpUtility.ParseQueryString(queryString)) { }//10
        //
        public override string ToString()
        {
            return BuildURL(this);
        }
        //
        public XmlDocument ToXML()
        {
            return BuildXML(this);
        }
        public XmlDocument ToXML(string name)
        {
            return BuildXML(this, name);
        }
        //
        public static string BuildURL(NameValueCollection nvc)
        {
            string[] ts;
            string t = string.Empty;
            for (int i = 0; i < nvc.Count; i++)
            {
                ts = nvc.GetValues(i);
                for (int j = 0; j < ts.Length; j++)
                {
                    t += "&";
                    t += HttpUtility.UrlEncode(nvc.GetKey(i));
                    t += "=";
                    t += HttpUtility.UrlEncode(ts[j]);
                }
            }
            t = t.Trim();
            if (!string.IsNullOrEmpty(t))
            {
                if (t[0] == '&') t = t.Substring(1);
                if (t[0] == '=') t = t.Substring(1);
                t = t.Replace("=&", "&");
                t = t.Replace("&=", "&");
            }
            return t;
        }
        //
        public static XmlDocument BuildXML(NameValueCollection nvc)
        {
            return BuildXML(nvc, "xml");
        }
        public static XmlDocument BuildXML(NameValueCollection nvc, string root)
        {
            string t;
            XmlDocument xml = new XmlDocument();
            xml.AppendChild(xml.CreateElement(root));
            for (int i = 0; i < nvc.Count; i++)
            {
                t = nvc.GetKey(i);
                if (string.IsNullOrEmpty(t)) t = root;
                xml.DocumentElement.SetAttribute(t, nvc[i]);
            }
            return xml;
        }
        //
    }
    //
}
//
